<template>
  <div class="main">
    <div class="nav" >
    <p v-for="(item , index) in arr" :key="index" @click="paixu(item , index)" :class=" indexs == index ? 'change' : ''  "> {{item}} </p>
  </div>
  <div class="list">
    <div class="shop" v-for="(item , index) in newlist ? newlist : list" :key="index">
      <img :src="item.picUrl" alt="">
      <div class="text">
        <h2>{{item.name}}</h2>
        <h3> {{item.monthSalesTip}} </h3>
        <h4> {{item.distance}} </h4>
      </div>
    </div>
  </div>
  </div>
</template>

<script>

import {shop_list} from '../api/api.js'

export default {
  data(){
    return {
      arr:['综合排序' , '距离最近' , '销量最高' , '筛选'],
      list:null,
      indexs:null,
      newlist:null

    }
  },
  methods:{
    paixu(item , index){
      if(this.indexs == index){
        this.indexs = null
        this.newlist = null
      }else{
        this.indexs = index
        if(item == '距离最近'){
          this.newlist = [...this.list]
          for(let i = 0 ; i < this.newlist.length ; i++){
            for(let j = i + 1 ; j < this.newlist.length ; j++){
              if(parseInt(this.newlist[i].distance) > parseInt(this.newlist[j].distance)){
                var temp
                temp = this.newlist[i]
                this.newlist[i] = this.newlist[j]
                this.newlist[j] = temp
              } 
            }
          }
        }else if(item == '销量最高'){
          this.newlist = [...this.list]
          for(let i = 0 ; i < this.newlist.length ; i++){
            for(let j = i + 1 ; j < this.newlist.length ; j++){
              if(parseInt(this.newlist[i].monthSalesTip.slice(2)) < parseInt(this.newlist[j].monthSalesTip.slice(2))){
                var temp
                temp = this.newlist[i]
                this.newlist[i] = this.newlist[j]
                this.newlist[j] = temp
              } 
            }
          }
        }else{
          this.newlist = null
        }
      }
    }
  },
  mounted(){
    shop_list().then((res) => {
      this.list = res.data.list
      console.log(this.list);
    })
  }
}
</script>

<style scoped>
    .nav{
        padding: 0 10px;
        height: 30px;
        display: flex;
        justify-content: space-around;
    }
    p{
        height: 20px;
        width: 21%;
        background-color: #eee;
        border-radius: 5px;
        margin-top: 5px;
        text-align: center;
    }
    .list{
      margin-top: 20px;
      height: 380px;
      overflow-y: auto;
    }
    .shop{
      margin-bottom: 10px;
      height: 120px;
      display: flex;
      box-sizing: border-box;
      padding: 10px;
    }
    .shop img{
      height: 100px;
      margin-top: 10px;
      width: 100px;
    }
    .shop .text{
      padding: 20px;
      box-sizing: border-box;
    }
    h2,h3,h4{
      height: 30px;
      font-size: 12px;
    }
    .change{
      color: rgb(235, 77, 77);
    }
</style>